namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using Xunit;

    public sealed class TenantControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantController>()
                .DerivesFrom<ResourceController>()
                .IsAbstractBaseClass()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TestTenantController());
        }

        [Fact]
        public void op_HtmlView_CultureInfo_object()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = 
                        "<tenant>" +
                            "<master-pages>" +
                                "<value name='html'>HtmlRepresentation.Default</value>" +
                            "</master-pages>" +
                        "</tenant>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "HtmlRepresentation.Default";
                string actual = ((new TestTenantController() as TenantController).HtmlView(Thread.CurrentThread.CurrentUICulture, new IResourceDummy()) as ViewResult).MasterName;

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_HtmlView_CultureInfo_objectNull()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml =
                        "<tenant>" +
                            "<master-pages>" +
                                "<value name='html'>HtmlRepresentation.Default</value>" +
                            "</master-pages>" +
                        "</tenant>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "HtmlRepresentation.Default";
                string actual = ((new TestTenantController() as TenantController).HtmlView(Thread.CurrentThread.CurrentUICulture, null as object) as ViewResult).MasterName;

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_View_CultureInfo_string_object()
        {
            string expected = "Default";
            string actual = ((new TestTenantController() as TenantController).View(Thread.CurrentThread.CurrentUICulture, expected, new IResourceDummy()) as ViewResult).MasterName;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_View_CultureInfo_stringEmpty_object()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => (new TestTenantController() as TenantController).View(Thread.CurrentThread.CurrentUICulture, string.Empty, new IResourceDummy()));
        }

        [Fact]
        public void op_View_CultureInfo_stringNull_object()
        {
            string expected = string.Empty;
            string actual = ((new TestTenantController() as TenantController).View(Thread.CurrentThread.CurrentUICulture, null as string, new IResourceDummy()) as ViewResult).MasterName;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_View_CultureInfo_string_objectNull()
        {
            string expected = "Default";
            string actual = ((new TestTenantController() as TenantController).View(Thread.CurrentThread.CurrentUICulture, expected, null as object) as ViewResult).MasterName;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_XhtmlView_CultureInfo_object()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml =
                        "<tenant>" +
                            "<master-pages>" +
                                "<value name='xhtml'>XhtmlRepresentation.Default</value>" +
                            "</master-pages>" +
                        "</tenant>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "XhtmlRepresentation.Default";
                string actual = ((new TestTenantController() as TenantController).XhtmlView(Thread.CurrentThread.CurrentUICulture, new IResourceDummy()) as ViewResult).MasterName;

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlView_CultureInfo_objectNull()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml =
                        "<tenant>" +
                            "<master-pages>" +
                                "<value name='xhtml'>XhtmlRepresentation.Default</value>" +
                            "</master-pages>" +
                        "</tenant>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "XhtmlRepresentation.Default";
                string actual = ((new TestTenantController() as TenantController).XhtmlView(Thread.CurrentThread.CurrentUICulture, null as object) as ViewResult).MasterName;

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}